//
//  WriteTopicTextAndImageTableCell.m
//  ArtDog
//
//  Created by dllo on 16/9/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "WriteTopicTextAndImageTableCell.h"
#import "BaseCollectionView.h"
#import "ImageViewCollectionCell.h"
#import "HLMacro.h"
#import "BaseViewController.h"
#import "UIView+Common.h"
#import "Masonry.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "BaseNavigationController.h"
#import "SelectAlbumViewController.h"
#import "AlbumNavigationController.h"
#import <Photos/Photos.h>
#import "CommunityWriteTopicController.h"
#import "ImageCollectViewController.h"

static CGFloat height;

@interface WriteTopicTextAndImageTableCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITextViewDelegate, UIGestureRecognizerDelegate>

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UILabel *placeholderLabel;


@property (nonatomic, strong) UIView *resignView;

@end

@implementation WriteTopicTextAndImageTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageViewCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"imageViewCollectionCell"];
    _textView.delegate = self;
    height = SCREEN_WIDTH / 4 + 110 + (SCREEN_WIDTH - 55) / 4;
}

- (void)tapAction
{
    [_textView resignFirstResponder];
}

- (void)setImageArray:(NSMutableArray *)imageArray
{
    _imageArray = imageArray;
    height = SCREEN_WIDTH / 4 + 110 + (SCREEN_WIDTH - 55) / 4 * ((_imageArray.count + 1) / 4 + 1) + (_imageArray.count + 1) / 4 * 5;
    [_collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_imageArray.count == 9) {
        return 9;
    }
    return _imageArray.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageViewCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageViewCollectionCell" forIndexPath:indexPath];
    if (indexPath.item == _imageArray.count && _imageArray.count != 9) {
        cell.image = @"add_image";
    } else {
        PHAsset *asset = _imageArray[indexPath.item];
        PHImageManager *imageManager = [[PHImageManager alloc]init];
        [imageManager requestImageForAsset:asset targetSize:cell.frame.size contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
            
            if (result) {
                cell.imageView.image = result;
            }
        }];

    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((SCREEN_WIDTH - 55) / 4, (SCREEN_WIDTH - 55) / 4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [self getVCRecently];
    if (indexPath.item == _imageArray.count && _imageArray.count != 9) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *photographAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"手机相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            SelectAlbumViewController *selAlbumVC = [[SelectAlbumViewController alloc]init];
            AlbumNavigationController *alubmNavi = [[AlbumNavigationController alloc]initWithRootViewController:selAlbumVC];
            alubmNavi.selectCount = 9 - _imageArray.count;
            alubmNavi.writeTopicVC = (CommunityWriteTopicController *)vc;
            [vc presentViewController:alubmNavi animated:YES completion:nil];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:photographAction];
        [alert addAction:albumAction];
        [alert addAction:cancelAction];
        UIViewController *vc = [self getVCRecently];
        [vc presentViewController: alert animated:YES completion:^{

        }];

    } else {
        ImageCollectViewController *imageCollectVC = [[ImageCollectViewController alloc]init];
        imageCollectVC.imageArray = _imageArray;
        imageCollectVC.indexPath = indexPath;
        [vc.navigationController pushViewController:imageCollectVC animated:YES];
    }
}

#pragma mark - textView delegate
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeholderLabel.hidden = NO;
    } else {
        _placeholderLabel.hidden = YES;
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    BaseViewController *vc = (BaseViewController *)[self getVCRecently];
    _resignView = [[UIView alloc]init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [_resignView addGestureRecognizer:tap];
    [vc.view addSubview:_resignView];
    [_resignView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(vc.view);
        make.top.equalTo(textView.mas_bottom);
    }];
    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [_resignView removeFromSuperview];
    return YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

+ (CGFloat)getHeight
{
    return height;
}

@end
